# Replication File for
# "Behavioral Consequences of Open Candidate Recruitment"
# Authors: Jochen Rehmert


### set directory, read data, load packages
setwd("...")

library(foreign);library(MASS)
library(sandwich);library(lmtest)
library(stargazer)

dat <- read.dta('kobo_activity.dta')

dat$kobo_f <- NA
dat$kobo_f[dat$kobo == 1 & dat$term <= 1] <- 1
dat$kobo_f[dat$kobo != 1 ] <- 0
dat$kobo_f[dat$kobo == 1 & dat$term >= 2] <- 0

###############################################################################
#                               Table A20                                     #
###############################################################################

dat$margin <- as.numeric(gsub("\\,", "\\.", dat$margin))
dat$distance_diet <- as.numeric(gsub("\\,", "\\.", dat$distance_diet))
dat$did <- as.numeric(gsub("\\,", "\\.", dat$did))


summary(b.nb.1 <- glm.nb(num_infra ~ ldp + sdp + yp + as.factor(period) + kobo + term + margin + distance_diet + government, data = dat ))
b.nb.1.se <- coeftest(b.nb.1, vcov=vcovHC(b.nb.1,type="HC0",cluster="pid"))[,2]
summary(a.nb.1 <- glm.nb(num_infra ~ ldp + sdp + yp + as.factor(period) + kobo_f + term  + margin + distance_diet + government, data = dat ))
a.nb.1.se <- coeftest(a.nb.1, vcov=vcovHC(a.nb.1,type="HC0",cluster="pid"))[,2]
summary(b.nb.2 <- glm.nb(num_infra ~  ldp + sdp + yp +as.factor(period) + kobo * did + term + margin + distance_diet + government , data = dat ))
b.nb.2.se <- coeftest(b.nb.2, vcov=vcovHC(b.nb.2,type="HC0",cluster="pid"))[,2]
summary(a.nb.2 <- glm.nb(num_infra ~ ldp + sdp + yp+ as.factor(period) + kobo_f * did+ term + margin + distance_diet + government , data = dat ))
a.nb.2.se <- coeftest(a.nb.2, vcov=vcovHC(a.nb.2,type="HC0",cluster="pid"))[,2]

summary(b.poi.1 <- glm(num_infra ~ ldp + sdp + yp + as.factor(period) + kobo + term + margin + distance_diet + government, data = dat, family = poisson ))
b.poi.1.se <- coeftest(b.poi.1, vcov=vcovHC(b.poi.1,type="HC0",cluster="pid"))[,2]
summary(a.poi.1 <- glm(num_infra ~ ldp + sdp + yp + as.factor(period) + kobo_f + term + margin + distance_diet + government, data = dat, family = poisson ))
a.poi.1.se <- coeftest(a.poi.1, vcov=vcovHC(a.poi.1,type="HC0",cluster="pid"))[,2]
summary(b.poi.2 <- glm(num_infra ~ ldp + sdp + yp + as.factor(period) + kobo * did + term + margin + distance_diet + government , data = dat, family = poisson ))
b.poi.2.se <- coeftest(b.poi.2, vcov=vcovHC(b.poi.2,type="HC0",cluster="pid"))[,2]
summary(a.poi.2 <- glm(num_infra ~ldp + sdp + yp + as.factor(period) + kobo_f * did + term + margin + distance_diet + government , data = dat, family = poisson ))
a.poi.2.se <- coeftest(a.poi.2, vcov=vcovHC(a.poi.2,type="HC0",cluster="pid"))[,2]


dat$sh_infra <- dat$num_infra/dat$num_pqs


summary(b.lm.1 <- lm(sh_infra ~ ldp + sdp + yp + as.factor(period) + kobo + term + margin + distance_diet + government, data = dat ))
b.lm.1.se <- coeftest(b.lm.1, vcov=vcovHC(b.lm.1,type="HC0",cluster="pid"))[,2]
summary(a.lm.1 <- lm(sh_infra ~ ldp + sdp + yp + as.factor(period) + kobo_f + term + margin + distance_diet + government, data = dat ))
a.lm.1.se <- coeftest(a.lm.1, vcov=vcovHC(a.lm.1,type="HC0",cluster="pid"))[,2]
summary(b.lm.2 <- lm(sh_infra ~ ldp + sdp + yp + as.factor(period) + kobo  * did+ term + margin + distance_diet + government , data = dat ))
b.lm.2.se <- coeftest(b.lm.2, vcov=vcovHC(b.lm.2,type="HC0",cluster="pid"))[,2]
summary(a.lm.2 <- lm(sh_infra ~ ldp + sdp + yp + as.factor(period) + kobo_f * did + term + margin + distance_diet + government, data = dat ))
a.lm.2.se <- coeftest(a.lm.2, vcov=vcovHC(a.lm.2,type="HC0",cluster="pid"))[,2]


stargazer(list(a.nb.1, b.nb.1, a.nb.2, b.nb.2, a.poi.1, b.poi.1, a.poi.2, b.poi.2, a.lm.1, b.lm.1, a.lm.2, b.lm.2),
          se = list(a.nb.1.se, b.nb.1.se, a.nb.2.se, b.nb.2.se, a.poi.1.se, b.poi.1.se, a.poi.2.se, b.poi.2.se, a.lm.1.se, b.lm.1.se, a.lm.2.se, b.lm.2.se),
          add.lines = list(c("Period Fixed-Effects", rep("YES", 12)),
                           c("Party Fixed-Effects", rep("YES", 12))),
          covariate.labels = c('k\\"{o}bo','k\\"{o}bo', "Population Density","Seniority",
                               "Margin","Distance from Diet, in km (log)", "Government",
                               'k\\"{o}bo $\\times$ Density', 'k\\"{o}bo $\\times$ Density', "Intercept"), omit = c("ldp","sdp","yp","period"))


###############################################################################
#                               Table A21                                     #
###############################################################################

summary(b.nb.1 <- glm.nb(num_agri ~ ldp + sdp + yp + as.factor(period) + kobo + term + margin + distance_diet + government, data = dat ))
b.nb.1.se <- coeftest(b.nb.1, vcov=vcovHC(b.nb.1,type="HC0",cluster="pid"))[,2]
summary(a.nb.1 <- glm.nb(num_agri ~ ldp + sdp + yp + as.factor(period) + kobo_f + term + margin + distance_diet + government, data = dat ))
a.nb.1.se <- coeftest(a.nb.1, vcov=vcovHC(a.nb.1,type="HC0",cluster="pid"))[,2]
summary(b.nb.2 <- glm.nb(num_agri ~ ldp + sdp + yp + as.factor(period) + kobo *agriculture + term + margin + distance_diet + government , data = dat ))
b.nb.2.se <- coeftest(b.nb.2, vcov=vcovHC(b.nb.2,type="HC0",cluster="pid"))[,2]
summary(a.nb.2 <- glm.nb(num_agri ~ ldp + sdp + yp + as.factor(period) + kobo_f * agriculture+ term + margin + distance_diet + government, data = dat ))
a.nb.2.se <- coeftest(a.nb.2, vcov=vcovHC(a.nb.2,type="HC0",cluster="pid"))[,2]

summary(b.poi.1 <- glm(num_agri ~ ldp + sdp + yp + as.factor(period) + kobo + term + margin + distance_diet + government, data = dat, family = poisson ))
b.poi.1.se <- coeftest(b.poi.1, vcov=vcovHC(b.poi.1,type="HC0",cluster="pid"))[,2]
summary(a.poi.1 <- glm(num_agri ~ ldp + sdp + yp+ as.factor(period) + kobo_f + term + margin + distance_diet + government, data = dat, family = poisson ))
a.poi.1.se <- coeftest(a.poi.1, vcov=vcovHC(a.poi.1,type="HC0",cluster="pid"))[,2]
summary(b.poi.2 <- glm(num_agri ~ ldp + sdp + yp + as.factor(period) + kobo * agriculture + term  + margin + distance_diet + government , data = dat, family = poisson ))
b.poi.2.se <- coeftest(b.poi.2, vcov=vcovHC(b.poi.2,type="HC0",cluster="pid"))[,2]
summary(a.poi.2 <- glm(num_agri ~ ldp + sdp + yp + as.factor(period) + kobo_f * agriculture + term + margin + distance_diet + government , data = dat, family = poisson ))
a.poi.2.se <- coeftest(a.poi.2, vcov=vcovHC(a.poi.2,type="HC0",cluster="pid"))[,2]


dat$sh_agri <- dat$num_agri/dat$num_pqs


summary(b.lm.1 <- lm(sh_agri ~ ldp + sdp + yp + as.factor(period) + kobo + term + margin + distance_diet + government, data = dat ))
b.lm.1.se <- coeftest(b.lm.1, vcov=vcovHC(b.lm.1,type="HC0",cluster="pid"))[,2]
summary(a.lm.1 <- lm(sh_agri ~ ldp + sdp + yp + as.factor(period) + kobo_f + term + margin + distance_diet + government, data = dat ))
a.lm.1.se <- coeftest(a.lm.1, vcov=vcovHC(a.lm.1,type="HC0",cluster="pid"))[,2]
summary(b.lm.2 <- lm(sh_agri ~ ldp + sdp + yp+ as.factor(period) + kobo * agriculture + term + margin + distance_diet + government, data = dat ))
b.lm.2.se <- coeftest(b.lm.2, vcov=vcovHC(b.lm.2,type="HC0",cluster="pid"))[,2]
summary(a.lm.2 <- lm(sh_agri ~ ldp + sdp + yp + as.factor(period) + kobo_f  * agriculture+ term + margin + distance_diet + government, data = dat ))
a.lm.2.se <- coeftest(a.lm.2, vcov=vcovHC(a.lm.2,type="HC0",cluster="pid"))[,2]


stargazer(list(a.nb.1, b.nb.1, a.nb.2, b.nb.2, a.poi.1, b.poi.1, a.poi.2, b.poi.2, a.lm.1, b.lm.1, a.lm.2, b.lm.2),
          se = list(a.nb.1.se, b.nb.1.se, a.nb.2.se, b.nb.2.se, a.poi.1.se, b.poi.1.se, a.poi.2.se, b.poi.2.se, a.lm.1.se, b.lm.1.se, a.lm.2.se, b.lm.2.se),
          add.lines = list(c("Period Fixed-Effects", rep("YES", 12)),
                           c("Party Fixed-Effects", rep("YES", 12))), omit = c("sdp","ldp","yp","period"),
          covariate.labels = c('k\\={o}bo','k\\={o}bo', "Laborforce Share in Agriculture (Agriculture)","Seniority",
                               "Margin","Distance from Diet, in km (log)", "Government",
                               'k\\={o}bo $\\times$ Agriculture', 'k\\={o}bo $\\times$ Agriculture', "Intercept"))





###############################################################################
#                               Table A22                                    #
###############################################################################


library(readstata13)

dat <- readstata13::read.dta13("words_MP.dta")


sort(table(dat$feature[dat$kobo == 0]),decreasing=TRUE)[1:21]
sort(table(dat$feature[dat$kobo == 1]),decreasing=TRUE)[1:21]

